﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using System.Windows.Forms;

/*
 * 杭州允博条码科技有限公司
 * 杭州聚识自动化科技有限公司
 * 说明：配置文件的操作
 * 创建时间：2022.11.18
 * 创建人：蔡文奇
 * 最后修改时间 2022.11.21
 */

namespace Yunbo.Common
{

    /// <summary>
    /// 配置文件操作类
    /// </summary>
    public class ConfigFileOperation
    {
        // 声明INI文件的写操作函数 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 默认配置文件路径
        /// </summary>
        private static string filePath = AppContext.BaseDirectory + "\\Setup.ini";

        /// <summary>
        /// 默认构造函数使用默认配置文件路径
        /// </summary>
        public ConfigFileOperation() { }

        /// <summary>
        /// 带参数构造函数使用自定义配置文件路径
        /// </summary>
        /// <param name="path"></param>
        public ConfigFileOperation(string path)
        {
            filePath = path;
        }

        /// <summary>
        /// 写入配置文件的value
        /// </summary>
        /// <param name="section">配置节</param>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        public static void WriteValue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, filePath);
        }
        /// <summary>
        /// 读取配置文件的value
        /// </summary>
        /// <param name="section">配置节</param>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            StringBuilder temp = new StringBuilder(255 * 10);
            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255 * 10, filePath);
            return temp.ToString();
        }


        #region 使用演示

        //static void Main(string[] args)
        //{
        //    Console.WriteLine("输入任意键写入配置。");
        //    Console.ReadKey();
        //    //写入ini
        //    Ini ini = new Ini("c:/config.ini");
        //    ini.Writue("Printer", "Darkness", "44");
        //    ini.Writue("Printer", "Speed", "199");

        //    //取出ini
        //    string strDarkness = ini.ReadValue("Printer", "Darkness");
        //    string strSpeed = ini.ReadValue("Printer", "Speed");
        //    Console.WriteLine("当前打印机的浓度是{0}，打印速度是{1}", strDarkness, strSpeed);
        //    Console.ReadKey();
        //}

        //写出结果：
        //[Printer]
        //Darkness=44
        //Speed=199

        #endregion


    }


}


